// pages/cart/index.js
import {getSetting, chooseAddress, openSetting, showModal, showToast} from "../../utils/asyncWx.js";
import regeneratorRuntime from "../../lib/runtime/runtime.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        address:{},
        cart:[],
        allChecked:false,
        totalPrice:0,
        totalNum:0
    },
    onShow(){
        const address = wx.getStorageSync("address");
        const cart = wx.getStorageSync("cart")||[];
        this.setData({address});
        this.setCart(cart);
    },
    /**
     * 点击收货地址
     * */ 
    async handleChooseAddress(){
        // wx.chooseAddress({
        //     success: (result) => {
        //         console.log(result);
        //     }
        // });
        //   wx.getSetting({
        //       success: (result) => {
        //           const scopeAddress = result.authSetting["scope.address"];
        //           if(scopeAddress === true || scopeAddress === undefined){
        //               wx.chooseAddress({
        //                   success: (result1) => {
        //                       console.log(result1);
        //                   }
        //               });
        //           }else{
        //               wx.openSetting({
        //                   success: (result2) => {
        //                       console.log(result2);
        //                       wx.chooseAddress({
        //                           success: (result3) => {
        //                               console.log(result3);
        //                           }
        //                       });
        //                   }
        //               });
        //           }
        //       }
        //   });
        try {
            const res1 = await getSetting();
            const scopeAddress = res1.authSetting["scope.address"];
            if(scopeAddress === false){
                await openSetting();
            }
            const address = await chooseAddress();
            address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
            wx.setStorageSync("address", address);
        } catch (error) {
            console.log(error);
        }
    },
    /**
     * 商品的选中
     * */ 
    handleItemChange(e){
        const goods_id = e.currentTarget.dataset.id;
        let cart = this.data.cart;
        let index = cart.findIndex(v=>v.goods_id === goods_id);
        cart[index].checked = !(cart[index].checked);
        this.setCart(cart);
    },
    /**
     * 设置购物车状态，重新计算底部栏,更新data数据和缓存
     * */ 
    setCart(cart){
        let allChecked = true;
        let totalPrice = 0;
        let totalNum = 0;
        cart.forEach(v=>{
            if(v.checked === true){
                totalPrice += v.num*v.goods_price;
                totalNum += v.num;
            }else{
                allChecked=false;
            }
        });
        allChecked = cart.length!==0?allChecked:false;
        this.setData({
            cart,
            allChecked,
            totalPrice,
            totalNum
        });
        wx.setStorageSync("cart", cart);
          
    },
    /**
     * 全选按钮
     * */ 
    handleItemAllCheck(){
        let {cart,allChecked} = this.data;
        allChecked = !allChecked;
        cart.forEach(v=>v.checked = allChecked);
        this.setCart(cart);
    },
    /**
     * 数字按钮实现 + - ；
     * */ 
    async handleItemNumEdit(e){
        const goods_id = e.currentTarget.dataset.id;
        const operation = e.currentTarget.dataset.operation;
        let {cart} = this.data;
        const index = cart.findIndex(v=>v.goods_id === goods_id);
        if(cart[index].num===1&&operation===-1){
            const res = await showModal({content:"您确定要删除吗？"});
            if(res.confirm){
                cart.splice(index,1);
                this.setCart(cart);
            }else if(res.cancel){}
        }else{
            cart[index].num += operation;
            this.setCart(cart);
        }
    },
    /**
     * 支付按钮的功能
     * */ 
    async handlepay(){
        const {address,totalNum} = this.data;
        if(!address.userName){
            await showToast({title:"请选择地址！"});
            return ;
        }
        if(totalNum===0){
            await showToast({title:"购物车里空空如也哦  ^_^"});
            return ;
        }
        wx.navigateTo({
            url: '/pages/pay/index'
        });
    }
})